<?php

namespace app\console\model;

use think\facade\Session;
use think\facade\Cache;
use think\facade\Config;
use think\Model;

/**
 * 管理员model
 * */
class Admin extends Model
{
    /**
     * 管理员列表
     * @param array $where
     * @return object
     */
    public static function page_lists($where, $group_id)
    {

        return self::alias('a')
            ->where($where)
            ->where(['a.status' => 1, 'a.is_del' => 0])
            ->join('tp_group g', 'a.belong_group_id = g.id')
            ->field('a.*,g.group_name')
            ->order('a.create_time desc,a.id desc')
            ->paginate('', false, ['query' => ['group_id' => $group_id]]);
    }

    /**
     * 管理员登录验证
     * @param string $username
     * @param string $password
     * @return array
     */
    public static function check_login($username, $password)
    {
        $user = self::where(['username' => $username])->field('id,password,salt,status,powers')->find();

        if (!$user) {
            return ['code' => 0, 'msg' => '用户名或密码错误'];
        }

        if ($user['password'] != encrypt($password, $user['salt'])) {
            return ['code' => 0, 'msg' => '用户名或密码错误'];
        }

        if ($user['status'] != 1) {
            return ['code' => 0, 'msg' => '账户已被禁用，请联系上级管理员'];
        }

        $user['username'] = $username;
        $user->visible(['id', 'username']);
        Session::set('consoler', $user, 'console');
        return ['code' => 1];
    }

    /**
     * 管理员操作权限
     * @param int $id
     * @param string $powers
     * @return array
     */
    public static function admin_powers($id, $powers = '')
    {
        $pw_cache = Cache::get('admin_powers_' . $id);
        if ($pw_cache == false) {
            if (empty($powers)) {
                $powers = self::where(['id' => $id])->value('powers');
            }
            $powers_arr = explode(',', $powers);
            Cache::set('admin_powers_' . $id, $powers_arr, 0);
            return $powers_arr;
        }
        return $pw_cache;
    }

    /**
     * 管理员左侧菜单
     * @return array
     */
    public static function user_left_bar()
    {
        $left_bar_cache = Cache::get('left_bar_' . ADMIN_ID);

        if ($left_bar_cache != false) {
            return $left_bar_cache;
        }

        $console_menu = Config::pull('console_menu');
        if (ADMIN_ID != 0) {
            $user_url = [];
            $admin_powers = static::admin_powers(ADMIN_ID);
            foreach ($console_menu as $key => $value) {
                if (!in_array($key, $admin_powers) || !$value['is_show']) {
                    unset($console_menu[$key]);
                }

                foreach ($value['son_menu'] as $kson => $vson) {
                    if (!in_array($key . '_' . $kson, $admin_powers) || !$vson['is_show']) {
                        unset($console_menu[$key]['son_menu'][$kson]);
                    } else {
                        isset($vson['url']) && $user_url[] = $vson['url'];

                        // handle 操作
                        if (isset($vson['handle'])) {
                            foreach ($vson['handle'] as $kh => $vh) {
                                if (in_array($key . '_' . $kson . '_' . $kh, $admin_powers)) {
                                    isset($vh['url']) && $user_url[] = $vh['url'];
                                }
                            }
                        }
                    }

                    if (isset($vson['sun_menu'])) {
                        foreach ($vson['sun_menu'] as $ksun => $vsun) {
                            if (!in_array($key . '_' . $kson . '_' . $ksun, $admin_powers) || !$vsun['is_show']) {
                                unset($console_menu[$key]['son_menu'][$kson]['sun_menu'][$ksun]);
                            } else {
                                isset($vsun['url']) && $user_url[] = $vsun['url'];

                                // handle 操作
                                if (isset($vsun['handle'])) {
                                    foreach ($vsun['handle'] as $kuh => $vuh) {
                                        if (in_array($key . '_' . $kson . '_' . $ksun . '_' . $kuh, $admin_powers)) {
                                            isset($vuh['url']) && $user_url[] = $vuh['url'];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Cache::set('user_url_' . ADMIN_ID, $user_url, 0);
        }

        Cache::set('left_bar_' . ADMIN_ID, $console_menu, 0);

        return $console_menu;
    }

    /**
     * 当前链接是否为合法链接
     * @param string $http_url
     * @return bool
     */
    public static function check_user_url($http_url)
    {
        if (ADMIN_ID == 0) return true;
        $has_cache = Cache::get('user_url_' . ADMIN_ID);
        if (!in_array($http_url, $has_cache)) return false;
        return true;
    }

    /**
     * 管理员修改密码
     * @param int $id
     * @param string $old_pass
     * @param string $new_pass
     * @return array
     */
    public static function repass($id, $old_pass, $new_pass)
    {
        $admin = self::field('password,salt')->where(['id' => $id])->find();
        if (encrypt($old_pass, $admin['salt']) != $admin['password']) {
            return ['code' => 0, 'msg' => '原始密码错误'];
        }
        self::update(['password' => encrypt($new_pass, $admin['salt'])], ['id' => $id], true);
        return ['code' => 1, 'msg' => '修改成功'];
    }

}